#!/bin/bash

echo "=== 编译器项目调试环境测试 ==="
echo

# 检查GDB是否可用
echo "1. 检查GDB调试器..."
if command -v gdb &> /dev/null; then
    echo "✓ GDB已安装: $(which gdb)"
    gdb --version | head -1
else
    echo "✗ GDB未安装，请先安装GDB"
    exit 1
fi
echo

# 检查编译器是否可执行
echo "2. 检查编译器..."
if [ -f "./compiler" ] && [ -x "./compiler" ]; then
    echo "✓ 编译器可执行文件存在"
    echo "  文件大小: $(ls -lh ./compiler | awk '{print $5}')"
else
    echo "✗ 编译器可执行文件不存在或不可执行"
    echo "  请运行: make clean && make all"
    exit 1
fi
echo

# 测试编译器基本功能
echo "3. 测试编译器基本功能..."
if ./compiler test19.c > test19_debug.s 2>&1; then
    echo "✓ 编译器成功编译test19.c"
    echo "  生成汇编文件: test19_debug.s"
    echo "  汇编文件大小: $(ls -lh test19_debug.s | awk '{print $5}')"
else
    echo "✗ 编译器编译test19.c失败"
    exit 1
fi
echo

# 测试GDB调试
echo "4. 测试GDB调试功能..."
echo "   创建简单的GDB测试脚本..."
cat > test_gdb.gdb << 'EOF'
set confirm off
set pagination off
break main
run test19.c
info breakpoints
quit
EOF

if gdb -batch -x test_gdb.gdb ./compiler 2>/dev/null; then
    echo "✓ GDB调试功能正常"
else
    echo "✗ GDB调试功能异常"
fi
echo

# 清理测试文件
rm -f test_gdb.gdb test19_debug.s

echo "=== 调试环境测试完成 ==="
echo
echo "现在您可以："
echo "1. 在VS Code中按 Ctrl+Shift+D 打开调试面板"
echo "2. 选择 '调试编译器 (test19.c)' 配置"
echo "3. 在代码中设置断点（点击行号左侧）"
echo "4. 按 F5 开始调试"
echo
echo "或者使用命令行调试："
echo "gdb ./compiler"
echo "break main"
echo "run test19.c"
echo
